﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace Wys.Control.CSharp
{
    public partial class FormMove : Component 
    {
       
        public FormMove() { }

        #region 设置停靠窗体
        public Form DockForm 
        {
            get 
            {
                return this._DockForm;
            }
            set 
            {              
                this._DockForm = value;
                if (DesignMode)
                {
                    return;
                }
                if (this._DockForm != null)
                {
                    this._DockForm.Load += new System.EventHandler(this.DockForm_Load);
                    this._DockForm.SizeChanged += new System.EventHandler(this.DockForm_SizeChanged);
                    this._DockForm.LocationChanged += new System.EventHandler(this.DockForm_LocationChanged);
                    _t = new System.Threading.Timer(new TimerCallback(TimerProc));
                    _t.Change(500, 300);
                }
                
            }
        }

        /// <summary>
        /// 要停靠窗体的初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DockForm_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        /// <summary>
        /// 要停靠窗体的位置改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DockForm_LocationChanged(object sender, EventArgs e)
        {
            getdockside();
            if (_IsOrg == false)
            {
                _lastBoard = this._DockForm.Bounds;
                _IsOrg = true;
            }
        }

        /// <summary>
        /// 要停靠窗体的大小改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DockForm_SizeChanged(object sender, EventArgs e)
        {
            if (_IsOrg == true && status == OFF)
            {
                _lastBoard = this._DockForm.Bounds;
            }
        }

        /// <summary>
        /// 要停靠窗体的关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DockForm_Closed(object sender, EventArgs e)
        {
            if(_t!=null)
            {
                _t.Change(0, Timeout.Infinite);
            }
        } 

        /// <summary>
        /// 计时器事件
        /// </summary>
        /// <param name="state"></param>
        private void TimerProc(object state)
        {
            FormRefresh();
        }  
        #endregion     

        private Form _DockForm;
        System.Threading.Timer _t;
        private AnchorStyles _dockside = AnchorStyles.None;
        private bool _IsOrg = false;
        private Rectangle _lastBoard;

        private const int Docking = 0;
        private const int Pre_docking = 1;
        private const int OFF = 2;
        private int status = 2;

        public void InitDock()
        {
            try
            {
                _dockside = AnchorStyles.Left;
                status = Docking;
                _lastBoard = this._DockForm.Bounds;
                _IsOrg = true;
            }
            catch (Exception)
            {
                
            }            
        }

        private void getdockside()
        {
            try
            {
                if (this._DockForm.Top <= 0)
                {
                    _dockside = AnchorStyles.Top;
                    if (this._DockForm.Bounds.Contains(Cursor.Position))
                        status = Pre_docking;
                    else
                        status = Docking;
                }
                else if (this._DockForm.Left <= 0)
                {
                    _dockside = AnchorStyles.Left;
                    if (this._DockForm.Bounds.Contains(Cursor.Position))
                        status = Pre_docking;
                    else
                        status = Docking;
                }
                else if (this._DockForm.Left >= Screen.PrimaryScreen.Bounds.Width - this._DockForm.Width)
                {
                    _dockside = AnchorStyles.Right;
                    if (this._DockForm.Bounds.Contains(Cursor.Position))
                        status = Pre_docking;
                    else
                        status = Docking;
                }
                else
                {
                    _dockside = AnchorStyles.None;
                    status = OFF;
                }
            }
            catch (Exception)
            {
                
            }            
        }
        public enum 任务栏位置 { 上 = 0, 下 = 1, 左 = 2, 右 = 3 }
        public struct 任务栏型
        {
            public 任务栏位置 位置;
            public int height;

        };

        public 任务栏型 Get任务栏()
        {
            任务栏型 任务栏temp;
            任务栏temp.位置 = 任务栏位置.上;
            任务栏temp.height = 24;

            Rectangle rt = SystemInformation.WorkingArea;
            Rectangle rt1 = Screen.PrimaryScreen.Bounds;
            if (rt.Y > 0)
            {
                任务栏temp.位置 = 任务栏位置.上;
                任务栏temp.height = Math.Abs(rt1.Height - rt.Height);
            }
            if (rt.X > 0)
            {
                任务栏temp.位置 = 任务栏位置.左;
                任务栏temp.height = Math.Abs(rt1.Width - rt.Width);
            }
            if (rt.Right < rt1.Width)
            {
                任务栏temp.位置 = 任务栏位置.右;
                任务栏temp.height = Math.Abs(rt1.Width - rt.Width);
            }
            if (rt.Bottom < rt1.Height)
            {
                任务栏temp.位置 = 任务栏位置.下;
                任务栏temp.height = Math.Abs(rt1.Height - rt.Height);
            }
            return 任务栏temp;
        }

        private void FormRefresh()
        {
            if (this._DockForm == null)
                return;
            if (this == null || _IsOrg == false)
            {
                return;
            }
            任务栏型 x1 = Get任务栏();
            try
            {
                //以保証最小化時,能回復到原先的狀態
                if (this._DockForm.WindowState != FormWindowState.Minimized)
                {
                    if (this._DockForm.Bounds.Contains(Cursor.Position))
                    {
                        switch (_dockside)
                        {
                            case AnchorStyles.Top:
                                if (status == Docking)
                                    this._DockForm.Location = new Point(this._DockForm.Location.X, 0);
                                break;
                            case AnchorStyles.Right:
                                if (status == Docking)
                                {
                                    if (x1.位置 == 任务栏位置.上)
                                    {
                                        this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this._DockForm.Width,x1.height- 1);
                                    }
                                    else
                                    {
                                        this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this._DockForm.Width, 1);
                                    }
                                }
                                //this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this._DockForm.Width, 1);
                                if (this._DockForm.Left < Screen.PrimaryScreen.Bounds.Width - this._DockForm.Width)
                                {
                                    if (_IsOrg == true && status == OFF)
                                    {
                                        if (this._DockForm.Bounds.Width != _lastBoard.Width || this._DockForm.Bounds.Height != _lastBoard.Height)
                                        {
                                            this._DockForm.Size = new Size(_lastBoard.Width, _lastBoard.Height);
                                        }
                                    }
                                }
                                break;
                            case AnchorStyles.Left:
                                if (status == Docking)
                                {
                                    if (x1.位置 == 任务栏位置.上)
                                    {
                                        this._DockForm.Location = new Point(0, x1.height);
                                    }
                                    else
                                    {
                                        this._DockForm.Location = new Point(0, 1);
                                    }
                                }
                                break;
                        }
                    }
                    else
                    {
                        switch (_dockside)
                        {
                            case AnchorStyles.Top:
                                this._DockForm.Location = new Point(this._DockForm.Location.X, (this._DockForm.Height - 5) * (-1));
                                break;
                            case AnchorStyles.Right:
                                this._DockForm.Size = new Size(this._DockForm.Width, Screen.PrimaryScreen.WorkingArea.Height);
                                //this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 5, 1);
                                if (x1.位置 == 任务栏位置.上)
                                {
                                    this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 5, x1.height - 1);
                                }                               
                                else 
                                {
                                    this._DockForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 5, 1);
                                }
                                break;
                            case AnchorStyles.Left:
                                this._DockForm.Size = new Size(this._DockForm.Width, Screen.PrimaryScreen.WorkingArea.Height);
                                if (x1.位置 == 任务栏位置.上)
                                {
                                    this._DockForm.Location = new Point((-1) * (this._DockForm.Width - 5), x1.height-1 );
                                }                               
                                else 
                                {
                                    this._DockForm.Location = new Point((-1) * (this._DockForm.Width - 5), 1);
                                }
                                break;
                            case AnchorStyles.None:
                                if (_IsOrg == true && status == OFF)
                                {
                                    if (this._DockForm.Bounds.Width != _lastBoard.Width || this._DockForm.Bounds.Height != _lastBoard.Height)
                                    {
                                        this._DockForm.Size = new Size(_lastBoard.Width, _lastBoard.Height);
                                    }
                                }
                                break;
                        }
                    }

                }
            }
            catch (Exception)
            {

            }            
        }
    }
}
